/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cafe.Controller;

import cafe.DataObject.Customer;
import cafe.Model.CustomerModel;
import hapax.TemplateDataDictionary;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.util.log.Log;

/**
 *
 * @author TieuHan
 */
public class CustommerController extends ControllerInterface {

    private static final org.eclipse.jetty.util.log.Logger LOG = Log.getLogger(CustommerController.class);
    CustomerModel model = CustomerModel.getInstance();

    @Override
    public String doAction(String action, HttpServletRequest request, HttpServletResponse response, String context) {

        switch (action) {
            case "additem":
                LOG.info("Add Customer");
                return addItem(request, response, context);
            case "getpageAdditem":
                return index(request, response, context, "/View/themkhachhang.xtm");
            case "delete":
                LOG.info("Delete Customer");
                return delete(request, response, context);
            case "getitem":
                break;
            /*
             Writer: ANNN@vng.com
             Date: 26/06/2014
             */
            case "edit":
                LOG.info("Edit Customer");
                return edit(request, response, context);
            case "update":
                LOG.info("Update Customer");
                return edit(request, response, context);
            /*
             End
             */
            default:
                LOG.info("Default Customer Action: Index");
                return index(request, response, context, "/View/khachhang.xtm");
        }
        return "";

    }

    /*
     Writer: ANNN@vng.com
     Date: 26/6/2014
     */
    @Override
    public String edit(HttpServletRequest request, HttpServletResponse response, String context) {
        if (request.getParameter("do").equals("edit")) {
            return RenderPage(request, response, context, "/View/Ajax/ajax_suakhachhang.xtm");
        } else {
            if (request.getParameter("do").equals("update")) {
                model.edit(request.getParameter("id"),
                        new Customer(
                                request.getParameter("name"),
                                request.getParameter("gender") == "1" ? false : true,
                                request.getParameter("phone")));

                return RenderPage(request, response, context, "/View/Ajax/ajax_khachhang.xtm");
            }
        }
        return null;
    }

    private String RenderPage(HttpServletRequest request, HttpServletResponse response, String context, String file) {
        Customer customer = model.getByID(request.getParameter("id"));
        TemplateDataDictionary cmDic = templateCollection.getTemplateDataDictionary();
        cmDic.setVariable("Customer.id", customer.getId());
        cmDic.setVariable("Customer.name", customer.getName());
        cmDic.setVariable("Customer.sex", customer.getSex() == true ? "Nam" : "Nữ");
        cmDic.setVariable("Customer.sexNam", customer.getSex() == true ? "checked='checked'" : "");
        cmDic.setVariable("Customer.sexNu", customer.getSex() == false ? "checked='checked'" : "");
        cmDic.setVariable("Customer.phone", customer.getPhone());
        return index(request, response, context, file);
    }

    /*
     End
     */
    

    @Override
    public String index(HttpServletRequest request, HttpServletResponse response, String context, String URL) {

        List<Customer> cm = (List<Customer>) model.getAll();

        for (Customer customer : cm) {
            TemplateDataDictionary cmDic = templateCollection.getTemplateDataDictionary().addSection("Customer");

            cmDic.setVariable("Customer.id", customer.getId());
            cmDic.setVariable("Customer.name", customer.getName());
            cmDic.setVariable("Customer.sex", customer.getSex() == true ? "Nam" : "Nữ");
            cmDic.setVariable("Customer.phone", customer.getPhone());
        }
        //set variable to section

        return super.index(request, response, context, URL); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public String addItem(HttpServletRequest request, HttpServletResponse response, String context) {

        model.insert(new Customer(request.getParameter("name"),
                request.getParameter("gender") == "1" ? false : true,
                request.getParameter("phone")));

        request = null;

        return index(request, response, context, "/View/khachhang.xtm");
    }

    @Override
    public String delete(HttpServletRequest request, HttpServletResponse response, String context) {

        model.delete(request.getParameter("id"));
        return index(request, response, context, "/View/khachhang.xtm");
    }

}
